package com.pet.service.impl;


import com.pet.context.BaseContext;
import com.pet.dto.web.institution.PetInstitutionApplicationDTO;
import com.pet.entity.PetInformation;
import com.pet.entity.institution.PetInstitutionApplication;
import com.pet.entity.institution.PetInstitutionInformation;
import com.pet.mapper.PetInstitutionMapper;
import com.pet.mapper.UserInformationMapper;
import com.pet.service.PetInstitutionService;
import com.pet.vo.web.PetInformationVO;
import com.pet.vo.web.institution.PetInstitutionApplicationVO;
import com.pet.vo.web.institution.PetInstitutionInformationVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class PetInstitutionServiceImpl implements PetInstitutionService {

    @Autowired
    private PetInstitutionMapper petInstitutionMapper;
    @Autowired
    private UserInformationMapper userInformationMapper;

    /**
     * 查询基地信息
     * @return
     */
    public List<PetInstitutionInformationVO> selectPetInstitutionInformation() {
        //创建vo list
        List<PetInstitutionInformationVO> voList = new ArrayList<>();
        //创建实体并获取
        List<PetInstitutionInformation> entityList = petInstitutionMapper.selectPetInstitutionInformation();
        //转换
        for (PetInstitutionInformation entity : entityList) {
            //创建vo
            PetInstitutionInformationVO vo = new PetInstitutionInformationVO();
            //赋值vo
            BeanUtils.copyProperties(entity, vo);
            //根据entity的Id 获取 图片数组
            List<String> imgList = petInstitutionMapper.selectInstitutionImage(entity.getId());
            vo.setInstitutionImg(imgList);
            voList.add(vo);
        }

        return voList;
    }

    /**
     * 查询机构动物
     * @param id
     * @return
     */
    public List<PetInformationVO> selectPetInformationAnimal(Integer id) {
        //创建vo list
        List<PetInformationVO> voList = new ArrayList<>();
        //创建实体并获取
        List<PetInformation> entityList = petInstitutionMapper.selectPetInformationAnimal(id);
        //转换
        for (PetInformation entity : entityList) {
            //创建vo
            PetInformationVO vo = new PetInformationVO();
            //赋值vo
            BeanUtils.copyProperties(entity, vo);
            //对 接种疫苗情况 是否驱虫情况 是否绝育情况进行判断
            if ( entity.getVaccine() == 1){
                vo.setVaccine("已接种疫苗");
            }else{
                vo.setVaccine("未接种疫苗");
            }

            if ( entity.getInsectRepellent() == 1){
                vo.setInsectRepellent("已驱虫");
            }else{
                vo.setVaccine("未驱虫");
            }

            if ( entity.getSterilization() == 1){
                vo.setSterilization("已绝育");
            }else{
                vo.setSterilization("未绝育");
            }
            //根据创建者id获取创建者姓名
            vo.setCreateUser(userInformationMapper.selectName(entity.getCreateUser()));

            voList.add(vo);
        }

        return voList;
    }

    /**
     * 申请机构
     * @param dto
     */
    public void applyInstitution(PetInstitutionApplicationDTO dto) {
        PetInstitutionApplication e = new PetInstitutionApplication();
        BeanUtils.copyProperties(dto,e);
        e.setApplicationUser(Math.toIntExact(BaseContext.getCurrentId()));

        petInstitutionMapper.insertInstitutionApplication(e);
    }

    /**
     * 查询机构申请进度
     * @return
     */
    public PetInstitutionApplicationVO selectApplicationStatus() {
        PetInstitutionApplication e = petInstitutionMapper.selectApplicationStatus(BaseContext.getCurrentId());
        PetInstitutionApplicationVO vo = new PetInstitutionApplicationVO();
        BeanUtils.copyProperties(e,vo);
        if (e.getStatus() == 1){
            vo.setStatus("审核通过");
        } else if (e.getStatus() == 2) {
            vo.setStatus("正在审核中，请耐心等待");
        }else{
            vo.setStatus("审核不通过，请查看失败原因");
        }
        vo.setApplicationUser(userInformationMapper.selectName(Math.toIntExact(BaseContext.getCurrentId())));

        return vo;
    }

    /**
     * 取消机构
     */
    public void deleteApplyInstitution() {
        petInstitutionMapper.deleteApplyInstitution(BaseContext.getCurrentId());
    }
}
